package com.hzit.proxy.message;


import com.alibaba.fastjson.JSONObject;
import com.hzit.net.util.WXNetUtil;
import com.hzit.proxy.base.AccessToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SendMessageProxy {
          private String myUrl="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+ AccessToken.getToken();
      public String sendTextMsg(String openid,String content){
          String msgtype = MsgType.TEXT.getMsgtype();
          Map<String,Object> map=new HashMap<>();
          map.put("touser",openid);
          map.put("msgtype",msgtype);
          Map<String,String> contentMap=new HashMap<>();
          contentMap.put("content",content);
          map.put("text",contentMap);
          String param = JSONObject.toJSONString(map);
          String result = WXNetUtil.post(myUrl, param);
          JSONObject jsonObject = JSONObject.parseObject(result);
          if(jsonObject.getString("errcode").equals("0")){
              return "OK";
          }

          return "FAILED";
      }

      public String sendNewsMsg(String openid,String title,String description,String url,String picurl){
          String msgtype = MsgType.NEWS.getMsgtype();
          Map<String,Object>map=new HashMap<>();
          map.put("touser",openid);
          map.put("msgtype",msgtype);
          Map<String, List<Map<String,String>>>newsMap=new HashMap<>();
          List<Map<String,String>>articles=new ArrayList<>();
          Map<String,String>articlesMap=new HashMap<>();
          articlesMap.put("title",title);
          articlesMap.put("description",description);
          articlesMap.put("url",url);
          articlesMap.put("picurl",picurl);
          articles.add(articlesMap);
          newsMap.put("articles",articles);
          map.put("news",newsMap);
          String param = JSONObject.toJSONString(map);
          String result = WXNetUtil.post(myUrl, param);
          JSONObject jsonObject = JSONObject.parseObject(result);
          if(jsonObject.getString("errcode").equals("0")){
              return "OK";
          }
          return "FAILED";
      }



}
